package com.duoketech.controller.web.order;

import com.duoketech.config.Constant;
import com.duoketech.controller.BaseController;
import com.duoketech.controller.web.ICRUD;
import com.duoketech.exception.ValidateException;
import com.duoketech.model.order.Order;
import com.duoketech.service.web.order.OrderService;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;

public class OrderController extends BaseController implements ICRUD {

    @Override
    public void listAll() {
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",20);
        Kv kv = Kv.by(Constant.MEMBER_ID_KEY,getShopMemberId());
        Page<Order> result = (Page<Order>) OrderService.ME.pageAllRecord(getModel(Order.class),pageNum,pageSize,kv);
        for(Order o:result.getList()){
            o.set(Order.STATUS,o.status());
            o.set(Order.DELIVERY_FEE,o.deliveryFee());
            o.set(Order.TOTAL_FEE,o.totalFee());
        }
        renderObject(result);
    }

    @Override
    public void queryById() {
        int id = getParaToInt("id",0);
        try {
            Order o= (Order) OrderService.ME.queryBy(id,getModel(Order.class),Kv.create());
//            o.set(Order.STATUS,o.status());
//            o.set(Order.DELIVERY_FEE,o.deliveryFee());
//            //o.set(Order.TOTAL_FEE,o.totalFee());
//            o.set(Order.TOTAL_WEIGHT,o.totalWeight());
//            o.put("total_fee_delivery",o.totalFeeWithDelivery());
            setAttr("order",o);
            setAttr("orderItems",OrderService.ME.queryOrderItemByOrderId(id));
            render("edit-"+viewModel()+".html");
            } catch (ValidateException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doAdd() {
        try {
            OrderService.ME.doAdd(getModel(Order.class),Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }

    }

    @Override
    public void doUpdate() {
        try {
            OrderService.ME.update(getModel(Order.class),Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    @Override
    public void doDelete() {
        int id = getParaToInt("id",0);
        try {
        OrderService.ME.deleteBy(id,getModel(Order.class),Kv.create());
        renderSuccess("操作成功");
        } catch (ValidateException e) {
        renderError(e.getMessage());
        }
    }
  }